package com.jlwl.houseservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jlwl.houseservice.entity.ArticleCategory;
import com.jlwl.houseservice.mapper.ArticleCategoryMapper;
import com.jlwl.houseservice.service.ArticleCategoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 文章分类Service实现类
 */
@Service
public class ArticleCategoryServiceImpl extends ServiceImpl<ArticleCategoryMapper, ArticleCategory> implements ArticleCategoryService {

    @Override
    public Page<ArticleCategory> pageList(Integer page, Integer size, Integer status) {
        LambdaQueryWrapper<ArticleCategory> queryWrapper = new LambdaQueryWrapper<>();
        
        // 状态筛选
        if (status != null) {
            queryWrapper.eq(ArticleCategory::getStatus, status);
        }
        
        // 按显示顺序排序
        queryWrapper.orderByAsc(ArticleCategory::getDisplayOrder);
        
        // 分页查询
        return page(new Page<>(page, size), queryWrapper);
    }

    @Override
    public List<ArticleCategory> getAllEnabledCategories() {
        return list(new LambdaQueryWrapper<ArticleCategory>()
                .eq(ArticleCategory::getStatus, 1)
                .orderByAsc(ArticleCategory::getDisplayOrder));
    }

    @Override
    @Transactional
    public boolean createCategory(ArticleCategory category) {
        category.setCreatedAt(LocalDateTime.now());
        category.setUpdatedAt(LocalDateTime.now());
        return save(category);
    }

    @Override
    @Transactional
    public boolean updateCategory(ArticleCategory category) {
        ArticleCategory existCategory = getById(category.getId());
        if (existCategory == null) {
            return false;
        }
        
        category.setUpdatedAt(LocalDateTime.now());
        return updateById(category);
    }

    @Override
    @Transactional
    public boolean deleteCategory(Integer id) {
        return removeById(id);
    }
} 